home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / ppm / ppmtosix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-06  |  5.2 KB  |  203 lines

  1. /* ppmtosix.c - read a portable pixmap and produce a color sixel file
  2. **
  3. ** Copyright (C) 1991 by Rick Vinci.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "ppm.h"
  14. #include "ppmcmap.h"
  15.  
  16. #define MAXVAL 100
  17. #define MAXCOLORS 256
  18.  
  19. #define DCS '\220'   /* Device Control String */
  20. #define ST  '\234'   /* String Terminator */
  21. #define CSI '\233'   /* Control String Introducer */
  22. #define ESC '\033'   /* Escape character */
  23.  
  24. static pixel** pixels;   /* stored ppm pixmap input */
  25. static colorhash_table cht;
  26. int margin;
  27.  
  28. void
  29. main( argc, argv )
  30.     int argc;
  31.     char* argv[];
  32.     {
  33.     FILE* ifp;
  34.     int argn, rows, cols, colors;
  35.     int Red, Grn, Blue, rownum, colnum;
  36.     int raw;
  37.     pixval maxval;
  38.     colorhist_vector chv;
  39.     char* usage = "[-raw] [-margin] [ppmfile]";
  40.  
  41.     ppm_init( &argc, argv );
  42.  
  43.     argn = 1;
  44.     raw = 0;
  45.     margin = 0;
  46.  
  47.     /* Parse args. */
  48.     while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
  49.     {
  50.     if ( pm_keymatch( argv[argn], "-raw", 2 ) )
  51.         raw = 1;
  52.     else if ( pm_keymatch( argv[argn], "-margin", 2 ) )
  53.         margin = 1;
  54.     else
  55.         pm_usage( usage );
  56.     ++argn;
  57.     }
  58.  
  59.     if ( argn < argc )
  60.     {
  61.     ifp = pm_openr( argv[argn] );
  62.     ++argn;
  63.     }
  64.     else
  65.     ifp = stdin;
  66.  
  67.     if ( argn != argc )
  68.     pm_usage( usage );
  69.  
  70.     /* Read in the whole ppmfile. */
  71.     pixels = ppm_readppm( ifp, &cols, &rows, &maxval );
  72.     pm_close( ifp );
  73.  
  74.     /* Print a warning if we're could to lose accuracy when rescaling colors. */
  75.     if ( maxval > MAXVAL )
  76.     pm_message(
  77.         "maxval is not %d - automatically rescaling colors", MAXVAL );
  78.  
  79.     /* Figure out the colormap. */
  80.     pm_message( "computing colormap..." );
  81.     chv = ppm_computecolorhist( pixels, cols, rows, MAXCOLORS, &colors );
  82.     if ( chv == (colorhist_vector) 0 )
  83.     pm_error( "too many colors - try doing a 'ppmquant %d'", MAXCOLORS );
  84.     pm_message( "%d colors found", colors );
  85.  
  86.     /* Make a hash table for fast color lookup. */
  87.     cht = ppm_colorhisttocolorhash( chv, colors );
  88.  
  89.     pm_message( "delivering sixel image..." );
  90.     WriteHeader();
  91.     WriteColorMap( chv, colors, maxval );
  92.     if ( raw == 1 )
  93.     WriteRawImage( cht, rows, cols );
  94.     else
  95.     WritePackedImage( cht, rows, cols );
  96.     WriteEnd();
  97.  
  98.     pm_close (stdout);
  99.  
  100.     exit( 0 );
  101.     }
  102.  
  103.  
  104. int
  105. WriteHeader()
  106.     {
  107.     if ( margin == 1 )
  108.     fprintf (stdout,  "%c%d;%ds", CSI, 14, 72 );
  109.     fprintf (stdout,  "%c", DCS );  /* start with Device Control String */
  110.     fprintf (stdout,  "0;0;8q" );   /* Horizontal Grid Size at 1/90" and graphics On */
  111.     fprintf (stdout,  "\"1;1\n" );  /* set aspect ratio 1:1 */
  112.     }
  113.  
  114. int
  115. WriteColorMap( chv, colors, maxval )
  116.     colorhist_vector chv;
  117.     int colors;
  118.     pixval maxval;
  119.     {
  120.     register int colornum;
  121.     pixel p;
  122.  
  123.     for ( colornum = 0; colornum < colors ; ++colornum )
  124.     {
  125.     p = chv[colornum].color;
  126.     if ( maxval != MAXVAL )
  127.         PPM_DEPTH( p, p, maxval, MAXVAL );
  128.     fprintf (stdout,  "#%d;2;%d;%d;%d", colornum,
  129.         (int) PPM_GETR( p ), (int) PPM_GETG( p ), (int) PPM_GETB( p ) );
  130.     }
  131.     fprintf (stdout,  "\n" );
  132.     }
  133.  
  134. int
  135. WriteRawImage( cht, rows, cols )
  136.     colorhash_table cht;
  137.     int rows, cols;
  138.     {
  139.     int rownum, colnum, b;
  140.     char* sixel = "@ACGO_";
  141.     register pixel* pP;
  142.  
  143.     for ( rownum = 0; rownum < rows; ++rownum )
  144.     {
  145.     b = rownum % 6;
  146.     for ( colnum = 0, pP = pixels[rownum]; colnum < cols; ++colnum, ++pP )
  147.         fprintf (stdout,  "#%d%c", ppm_lookupcolor(cht, pP), sixel[b] );
  148.     fprintf (stdout,  "$\n" );   /* Carriage Return */
  149.     if ( b == 5 )
  150.         fprintf (stdout,  "-\n" );   /* Line Feed (one sixel height) */
  151.     }
  152.     }
  153.  
  154. int
  155. WritePackedImage( cht, rows, cols )
  156.     colorhash_table cht;
  157.     int rows, cols;
  158.     {
  159.     int rownum, colnum, b, repeat, thiscolor, nextcolor;
  160.     char* sixel = "@ACGO_";
  161.     register pixel* pP;
  162.  
  163.     for ( rownum = 0; rownum < rows; ++rownum )
  164.     {
  165.     b = rownum % 6;
  166.     repeat = 1;
  167.     for ( colnum = 0, pP = pixels[rownum]; colnum < cols; ++colnum, ++pP )
  168.         {
  169.         thiscolor = ppm_lookupcolor(cht, pP);
  170.         if ( colnum == cols -1 )   /* last pixel in row */
  171.         if ( repeat == 1 )
  172.             fprintf (stdout,  "#%d%c", thiscolor, sixel[b] );
  173.         else
  174.             fprintf (stdout,  "#%d!%d%c", thiscolor, repeat, sixel[b] );
  175.         else   /* not last pixel in row */
  176.         {
  177.         nextcolor =  ppm_lookupcolor(cht, pP+1);
  178.         if ( thiscolor == nextcolor )
  179.             ++repeat;
  180.         else
  181.             if ( repeat == 1 )
  182.             fprintf (stdout,  "#%d%c", thiscolor, sixel[b] );
  183.             else
  184.             {
  185.             fprintf (stdout,  "#%d!%d%c", thiscolor, repeat, sixel[b] );
  186.             repeat = 1;
  187.             }
  188.         }
  189.         }   /* end column loop */
  190.     fprintf (stdout,  "$\n" );   /* Carriage Return */
  191.     if ( b == 5 )
  192.         fprintf (stdout,  "-\n" );   /* Line Feed (one sixel height) */
  193.     }
  194.     }
  195.  
  196. int
  197. WriteEnd()
  198.     {
  199.     if ( margin == 1 )
  200.     fprintf (stdout,  "%c%d;%ds", CSI, 1, 80 );
  201.     fprintf (stdout,  "%c\n", ST );
  202.     }
  203.